package cn.net.xyan.ydpt

import cn.net.xyan.blossom.core.service.ConfigService
import cn.net.xyan.blossom.core.service.LogService
import cn.net.xyan.blossom.kotlin.json
import cn.net.xyan.yypt.dto.CallbackBizContentRsp
import cn.net.xyan.yypt.service.PayOrderCallbackService
import com.wondersgroups.yypt.CommitUpdateOrderReq
import com.wondersgroups.yypt.OrderController
import com.wondersgroups.yypt.OrderService
import org.springframework.beans.factory.annotation.Autowired
import java.net.URL
import javax.annotation.PostConstruct

class YYPTCallbackImpl : PayOrderCallbackService {

    companion object {
        const val KeyYYPTWSDL = "KeyYYPTWSDL"

        const val StatusPay = 1
    }

    @Autowired
    lateinit var configService: ConfigService

    @Autowired
    lateinit var logService: LogService

    lateinit var port: OrderController

    @PostConstruct
    fun setup() {
        configService.setupVariable(KeyYYPTWSDL, "http://192.168.203.90:8080/test/cxf/order?wsdl", "万达平台订单支付服务WSDL地址")

        val wsdl = configService.queryValue(KeyYYPTWSDL)

        val url = URL(wsdl)

        port = OrderService(url).orderControllerImplPort
    }

    override fun callback(notify: CallbackBizContentRsp): String {
        val req = CommitUpdateOrderReq()

        req.orderID = notify.outTradeNo?.toLong()
        req.payVendorNo = notify.tradeNo
        req.payFee = notify.totalAmount?.toFloat()

        req.payVendor = notify.payVendor

        req.status = StatusPay

        var rsp = port.updateOrder(req)

        logService.saveLog("callback", rsp?.json ?: "")

        return if (rsp.successStatus > 0)
            "success"
        else rsp.attach
    }

}